package com.psd.demo.common;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.log4j.Logger;







public class DBUtils {
	private static Logger logger = Logger.getLogger(DBUtils.class);
	
	
	protected static Properties PROPS = new Properties();
	static {
		final InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream(
		        "jdbc.properties");
		try {
			PROPS.load(inputStream);
		} catch(IOException e1) {
			logger.debug("--------------获取JDBC配置文件失败---------------------------");
			e1.printStackTrace();
		}
	}
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception 
	 */
	public Connection getConnection() throws Exception{
		Class.forName(PROPS.getProperty("jdbc.driverClassName"));
		return DriverManager.getConnection(PROPS.getProperty("jdbc.url"), PROPS.getProperty("jdbc.username"), PROPS.getProperty("jdbc.password"));

	}
	public static void close(Connection conn) throws SQLException {
		if (conn != null)
			conn.close();
	}

	public static void close(ResultSet rs) throws SQLException {
		if (rs != null)
			rs.close();
	}

	public static void close(Statement stmt) throws SQLException {
		if (stmt != null)
			stmt.close();
	}

	
	
}
